<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div style="margin-top: 20px;">

    <div class="search_block">
        搜索：
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input class="layui-input" id="s_name" name="s_name"  placeholder="按客户名称" autocomplete="off">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input class="layui-input" id="s_code" name="s_code"  placeholder="按客户编号" autocomplete="off">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <button class="layui-btn" id="search_btn" data-type="search">搜索</button>
                <label class="tip">${stateMsg}</label>
            </div>
        </div>
        <button class="layui-btn" style="float: right;padding-right: 10px" id="add_btn" data-type="add">添加新客户</button>
    </div>

    <table class="layui-hide" id="customers_table" lay-filter="customers_table"></table>

    <%--操作条--%>
    <script type="text/html" id="tool_bar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑信息</a>
    </script>

    <script>
        layui.use(['form','table','layer','util'],function () {
            var form = layui.form,
                table = layui.table,
                layer=layui.layer,
                $=layui.jquery;
            form.verify({
                name:function (value) {
                    if(value==""){
                        return '客户名称不能为空';
                    }
                }
                ,address:function (value) {
                    if(value.length<=0){
                        return '客户地址不能为空';
                    }
                }
            });
            form.on('submit(check)', function(data){
                return true;
            });

            table.render({
                elem: '#customers_table'
                ,even:true
                ,url:'/back/customer/load'
                ,cols: [[
                    {field:'name',width:120,title:'客户姓名',sort:true}
                    ,{field:'code',width:120, title: '客户编号'}
                    ,{field:'phone',width:120, title: '手机'}
                    ,{field:'address', title: '地址'}
                    ,{fixed: 'right',title:'操作', width:178, align:'center', toolbar: '#tool_bar'}
                ]]
                ,id:'id'
                ,page: true
                ,limit:10
            });


            //操作按钮监听模块
            table.on('tool(customers_table)',function (obj) {
                var data=obj.data;
                var event=obj.event;
                if(event=='edit'){

                    layer.open({
                        title:'修改页面'
                        ,type: 1
                        ,skin: 'layui-layer-lan'
                        ,area: ['330px','350px']
                        , content: $('#customer_form')
                        ,success: function(layero, index){
                            $('#customer_form').attr('action','${pageContext.request.contextPath}/back/customer/modifyInfo');
                            $('#id').val(data.id);
                            $('#code').val(data.code);
                            $('#name').val(data.name);
                            $('#phone').val(data.phone);
                            $('#address').val(data.address);
                            $('#btn').text("提交修改");
                            form.render();
                        }
                    });
                }
            });

            //搜索添加模块
            var $ = layui.$, active = {
                search: function(){
                    var s_name = $('#s_name').val();
                    var s_code=$('#s_code').val();
                    table.reload('id', {
                        where: {
                            s_name:s_name,
                            s_code:s_code
                        }
                        ,page: {
                            curr:1
                        }

                    });
                }
                ,add:function () {
                    layer.open({
                        title:'新客户信息录入'
                        ,type: 1
                        ,skin: 'layui-layer-lan'
                        ,area: ['330px','350px']
                        , content: $('#customer_form')
                        ,success: function(layero, index){
                            $('#customer_form').attr('action','${pageContext.request.contextPath}/back/customer/add');
                            $('#id').val("");
                            $('#code').val("");
                            $('#name').val("");
                            $('#phone').val("");
                            $('#address').val("");
                            $('#btn').text("提交添加");
                            form.render();
                        }
                    })
                }
            };

            $('#search_btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

            $('#add_btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });

    </script>

</div>

    <form id="customer_form" style="display: none" class="layui-form" action="#" method="post">
        <input type="hidden" id="id" name="id"/>
        <div class="info_form_body">
            <div class="layui-form-item">
                <label class="layui-form-label">客户编号:</label>
                <div class="layui-input-block">
                    <input id="code"  type="text" name="code"   lay-verify="required" placeholder="客户编号" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">客户名称:</label>
                <div class="layui-input-block">
                    <input id="name"  type="text" name="name"   lay-verify="name" placeholder="客户名称" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">电话:</label>
                <div class="layui-input-block">
                    <input id="phone" type="text"  name="phone"  lay-verify="required" placeholder="电话" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">地址:</label>
                <div class="layui-input-block">
                    <input id="address" type="text"  name="address"  lay-verify="address" placeholder="地址" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button id="btn" lay-submit="" lay-filter="check" class="layui-btn"></button>
                </div>
            </div>
        </div>
    </form>